<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="model.User"%>
<%@ page import="manager.UserManager"%>
<%@ page import="util.ConstantStringTable"%>
<%
	boolean islogin = false;
	//Let's check if the user already login.
	if(session != null && session.getAttribute(ConstantStringTable.LOGIN_USER)!=null){
		islogin = true;
	}else{
		//Well the user has not yet log in.
		//Let's see if there's a record in the cookie.
		Cookie cookies[] = request.getCookies();
		if(cookies!=null)
		for(Cookie cookie:cookies){
			if(cookie.getName().equals(ConstantStringTable.LOGIN_COOKIE)){
				
				//We found the cookie,try to login.
				//Cookie format is    username:md5Password
				String val[] = cookie.getValue().split(":");
				String username = val[0];
				String md5Password = val[1];
				try{
					User user = UserManager.getUserManager().login(username,md5Password);
					session.setAttribute(ConstantStringTable.LOGIN_USER,user);
					islogin = true;
					break;
				}catch(Exception exp){
					//attemp failed.
					continue;
				}
			}
		}
	}
	
	if(!islogin){
		//Redirect to the main page.
		response.sendRedirect("index.jsp");
	}
%>

